# -*- coding: utf-8 -*-
from autoscrapy import plugin_load
from autoscrapy.utils import log


LOG = log.get_logging(__name__)


class linkFilter(plugin_load.extensionManager):
    TYPE = 'mongo'

    def __init__(self, context):
        super(linkFilter, self).__init__('links')
        self._context = context
        self._link_plugin = self._plugins[self.TYPE](context)

    @property
    def link(self):
        return self._link_plugin

    def is_duplicate_url(self, url):
        try:
            return self.link.is_duplicate_url(url)
        except Exception as e:
            LOG.error("duplicate url %s fail", url)
            LOG.exception("duplicate url occure exception %s", e)
        return False

    def add_url(self, url):
        if not self.is_duplicate_url(url):
            try:
                self.link.add_url(url)
            except Exception as e:
                LOG.error(" add url %s fail", url)
                LOG.exception("add url occure exception %s", e)
